package cn.psh.org.controller;

import cn.psh.basic.util.ExcelUtil;
import cn.psh.basic.util.exception.BusinessException;
import cn.psh.org.domain.ShopAuditLog;
import cn.psh.org.service.IShopService;
import cn.psh.org.domain.Shop;
import cn.psh.org.query.ShopQuery;
import cn.psh.basic.util.AjaxResult;
import cn.psh.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addAndUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.del(id);
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.queryById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Shop> list() {

        return shopService.queryAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return shopService.queryData(query);
    }



    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("系统繁忙，骚后重试！！");
        }
    }
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("通过失败！" + e.getMessage());
        }
    }
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("驳回失败！" + e.getMessage());
        }
    }


    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/import")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> list = ExcelUtil.importExcel(file,0,1,Shop.class);
        System.out.println("导入数据一共【"+list.size()+"】行");
        for (Shop shop:list) {
            System.out.println(shop);
        }
    }

    @GetMapping("/active/{id}")
    public void active(@PathVariable("id") long id){

        shopService.active(id);

    }



}
